package com.ruoyi.exam.controller;

import java.io.IOException;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.exam.domain.ExamUniversityScore;
import com.ruoyi.exam.domain.model.UniversityScoreModel;
import com.ruoyi.exam.service.IExamUniversityScoreService;
import com.ruoyi.exam.service.impl.ExamUniversityScoreServiceImpl;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.Seq;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 学校信息Controller
 * 
 * @author wky
 * @date 2024-09-09
 */
@RestController
@RequestMapping("/exam/score")
@Tag(name = "【学校信息】管理")
public class ExamUniversityScoreController extends BaseController {
    @Autowired
    private ExamUniversityScoreServiceImpl examUniversityScoreService;

    /**
     * 查询学校信息列表
     */
    @Operation(summary = "查询学校信息列表")
    @PreAuthorize("@ss.hasPermi(' exam:score:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExamUniversityScore examUniversityScore) {
        startPage();
        List<ExamUniversityScore> list = examUniversityScoreService.selectExamUniversityScoreList(examUniversityScore);
        return getDataTable(list);
    }

    /**
     * 导出学校信息列表
     */
    @Operation(summary = "导出学校信息列表")
    @PreAuthorize("@ss.hasPermi(' exam:score:export')")
    @Log(title = "学校信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamUniversityScore examUniversityScore) {
        List<ExamUniversityScore> list = examUniversityScoreService.selectExamUniversityScoreList(examUniversityScore);
        ExcelUtil<ExamUniversityScore> util = new ExcelUtil<ExamUniversityScore>(ExamUniversityScore.class);
        util.exportExcel(response, list, "学校信息数据");
    }

    /**
     * 获取学校信息详细信息
     */
    @Operation(summary = "获取学校信息详细信息")
    @PreAuthorize("@ss.hasPermi(' exam:score:query')")
    @GetMapping(value = "/{infoId}")
    public AjaxResult getInfo(@PathVariable("infoId") Long infoId) {
        return success(examUniversityScoreService.selectExamUniversityScoreByInfoId(infoId));
    }

    /**
     * 新增学校信息
     */
    @Operation(summary = "新增学校信息")
    @PreAuthorize("@ss.hasPermi(' exam:score:add')")
    @Log(title = "学校信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExamUniversityScore examUniversityScore) {
        List<UniversityScoreModel> detailScore = examUniversityScore.getDetailScore();
        examUniversityScore.setExtraInfo(JSON.toJSONString(detailScore));
        return toAjax(examUniversityScoreService.insertExamUniversityScore(examUniversityScore));
    }

    /**
     * 修改学校信息
     */
    @Operation(summary = "修改学校信息")
    @PreAuthorize("@ss.hasPermi(' exam:score:edit')")
    @Log(title = "学校信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExamUniversityScore examUniversityScore) {
        return toAjax(examUniversityScoreService.updateExamUniversityScore(examUniversityScore));
    }

    /**
     * 删除学校信息
     */
    @Operation(summary = "删除学校信息")
    @PreAuthorize("@ss.hasPermi(' exam:score:remove')")
    @Log(title = "学校信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public AjaxResult remove(@PathVariable(name = "infoIds") Long[] infoIds) {
        return toAjax(examUniversityScoreService.deleteExamUniversityScoreByInfoIds(infoIds));
    }

    @PostMapping("/importTemplate")
    public void getImportTemplate(HttpServletResponse response) {
        ExcelUtil<UniversityScoreModel> util = new ExcelUtil<UniversityScoreModel>(UniversityScoreModel.class);
        util.importTemplateExcel(response, "分数数据");
    }

    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file, @RequestParam Long infoId) throws IOException {
        ExamUniversityScore info = examUniversityScoreService.selectExamUniversityScoreByInfoId(infoId);
        if (info == null) {
            throw new ServiceException("未知信息");
        }
        ExcelUtil<UniversityScoreModel> util = new ExcelUtil<UniversityScoreModel>(UniversityScoreModel.class);
        List<UniversityScoreModel> infoList = util.importExcel(file.getInputStream());
        for (UniversityScoreModel item : infoList) {
            item.setItemId(Seq.getId().toString());
        }
        info.setExtraInfo(JSON.toJSONString(infoList));
        examUniversityScoreService.updateExamUniversityScore(info);
        return success();
    }
}
